// @ts-nocheck
import React, { useEffect, useState } from "react";
import { NavBar, Rate, TextArea, Toast } from "antd-mobile";
import { useNavigate, useLocation } from "react-router-dom";
import "./EvaluationPublication.less";
import { evaluationSubmission } from "../../service";
import { getCurrentUserInfo } from "@/utils/authority";
import { safeParseJSON } from "@/utils";

const EvaluationPublication: React.FC<any> = () => {
	const navigate = useNavigate();
	const location = useLocation();
	const itemData = location?.state;
	const userId = getCurrentUserInfo()?.data?.id;

	const [rateValue, setRateValue] = useState("");
	const [testValue, setTestValue] = useState("");
	const getListData = async () => {
		const params = {
			taskId: itemData?.id,
			userId: userId,
			message: testValue,
			processInstanceId: itemData?.processInstanceId,
			score: rateValue,
		};
		if (rateValue) {
			try {
				const { code } = await evaluationSubmission(params);
				if (code == 200) {
					Toast.show("提交评价成功");
					navigate("/user/trends/evaluate");
				}
			} catch {}
		}else{
			Toast.show('你还未评分!')
		}
	};
	const onFnish = () => {
		getListData();
	};
	return (
		<div className="EvaluationPublication-contarnier">
			<div className="header">
				<div>
					<NavBar onBack={() => navigate("/user/trends/evaluate")}>
						<div className="commontitle">发表评价</div>
					</NavBar>
				</div>
			</div>
			<div className="EvaluationPublication-card">
				<div className="EvaluationPublication-box">
					<div className="EvaluationPublication-box-card">
						<div className="EvaluationPublication-box-card-title">
							<div>{safeParseJSON(itemData?.formCommitData)?.questiontitle || ""}</div>
							<div className={itemData?.processDefinitionName == "社区服务流程" ? "service" : "event"}>
								{itemData?.processDefinitionName == "社区服务流程" ? "服务" : "事件"}
							</div>
						</div>
						<div className="EvaluationPublication-box-card-time">{itemData?.createTime}</div>
						<hr />
						<div className="EvaluationPublication-box-card-mark">
							<span>评分：</span>
							<Rate onChange={(val) => setRateValue(parseInt(val))} />
							<span className="mark-val">{rateValue ? `${rateValue}.0` : rateValue}</span>
						</div>
						<TextArea
							placeholder="请输入评价内容..."
							rows={5}
							onChange={(val) => setTestValue(val)}
						/>
					</div>
				</div>
			</div>
			<div
				className="EvaluationPublication-buttom"
				onClick={() => onFnish()}>
				提交
			</div>
		</div>
	);
};

export default EvaluationPublication;
